//Demo
var http = "http://www.esunrising.net:8099/ords/kzks/api/";

function layout(event) {
  var layer = layui.layer;

  layer.open({
    title: "修改密码",
    type: 1,
    area: ["520px", "455px"],
    skin: "demo-class",
    content:
      '<form class="layui-form" action="">' +
      '<div class="layui-form-item">' +
      "  <p>旧密码</p>" +
      ' <div class="layui-input-inline">' +
      ' <input type="password" id="oldpassword" name="oldpassword" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">' +
      ' <div class="icon-css-view">' +
      ' <img id="iconShowViewone" src="./img/usermessage/eye.svg">' +
      '    <img id="iconHiddenViewone" src="./img/usermessage/eye-off.svg">' +
      "    </div>" +
      "</div>" +
      "  </div>" +
      '<div class="layui-form-item">' +
      "  <p>新密码</p>" +
      ' <div class="layui-input-inline">' +
      ' <input type="password" id="newpassword" name="newpassword" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">' +
      ' <div class="icon-css-view">' +
      ' <img id="iconShowViewtwo" src="./img/usermessage/eye.svg">' +
      '    <img id="iconHiddenViewtwo" src="./img/usermessage/eye-off.svg">' +
      "    </div>" +
      "</div>" +
      "  </div>" +
      '<div class="layui-form-item">' +
      "  <p>确认密码</p>" +
      ' <div class="layui-input-inline">' +
      ' <input type="password" id="nnewpassword" name="nnewpassword" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">' +
      ' <div class="icon-css-view">' +
      ' <img id="iconShowViewthree" src="./img/usermessage/eye.svg">' +
      '    <img id="iconHiddenViewthree" src="./img/usermessage/eye-off.svg">' +
      "    </div>" +
      "</div>" +
      "  </div>" +
      '  <div class="layui-form-item" class="typical_contentchangetwo">' +
      '  <div class="layui-input-block">' +
      '  <a type="reset" style="cursor:pointer;"  class="layui-layer-close" >' +
      '  <img src="./img/usermessage/icon_Undo@2x.png" style="width:12px; ">取消</a>' +
      '<a class="layui-btn"   lay-submit lay-filter="changepasswordd" class="layui-btn layui-disabled" >' +
      '  <img src="./img/usermessage/icon_save@2x.png" style="width:12px; ">保存</a>' +
      "  </div>" +
      " </div>" +
      "</form>",
  });
  layer.config({
    skin: "demo-class",
  });
}
layui.use(["layer", "laydate", "form", "table", "laytpl"], function () {
  var form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    laytpl = layui.laytpl,
    table = layui.table;

  //修改多选框变为单选框
  var laydate = layui.laydate;
  form.on("checkbox(oneCheck)", function (data) {
    $("input[name='sex']").prop("checked", false);
    $(this).prop("checked", true);
    form.render("checkbox");
  });
  form.render("checkbox");

  //密码可视化
  $("body").on("click", "#iconShowViewone", "#oldpassword", function () {
    $("#oldpassword").attr("type", "text");
    $("#iconShowViewone").css("display", "none");
    $("#iconHiddenViewone").css("display", "block");
    form.render();
  });

  $("body").on("click", "#iconHiddenViewone", "#oldpassword", function () {
    $("#oldpassword").attr("type", "password");
    $("#iconShowViewone").css("display", "block");
    $("#iconHiddenViewone").css("display", "none");
    form.render();
  });

  $("body").on("click", "#iconShowViewtwo", "#newpassword", function () {
    $("#newpassword").attr("type", "text");
    $("#iconShowViewtwo").css("display", "none");
    $("#iconHiddenViewtwo").css("display", "block");
    form.render();
  });

  $("body").on("click", "#iconHiddenViewtwo", "#newpassword", function () {
    $("#newpassword").attr("type", "password");
    $("#iconShowViewtwo").css("display", "block");
    $("#iconHiddenViewtwo").css("display", "none");
    form.render();
  });

  $("body").on("click", "#iconShowViewthree", "#nnewpassword", function () {
    $("#nnewpassword").attr("type", "text");
    $("#iconShowViewthree").css("display", "none");
    $("#iconHiddenViewthree").css("display", "block");
    form.render();
  });

  $("body").on("click", "#iconHiddenViewthree", "#nnewpassword", function () {
    $("#nnewpassword").attr("type", "password");
    $("#iconShowViewthree").css("display", "block");
    $("#iconHiddenViewthree").css("display", "none");
    form.render();
  });
});

function changemessgae(event) {
  var form = layui.form;

  var a = document.getElementsByClassName("typical_contentchange")[0];
  a.style.display = "none";

  var b = document.getElementsByClassName("typical_contentchangeone")[0];
  b.style.display = "block";

  var c = document.getElementsByClassName("typical_content")[0];
  c.style.display = "none";

  var d = document.getElementsByClassName("typical_contentone")[0];
  d.style.display = "block";

  $.ajax({
    type: "get",
    url: http + "user/" + localStorage.getItem("user_id"),
    dataType: "json",
    async: true,
    success: function (result) {
      document.getElementById("hiddenname").value = result.name;
      document.getElementById("hiddenrole").value = result.role_str;

      document.getElementById("showuernamee").innerHTML = result.username;
      document.getElementById("showrolee").innerHTML = result.role_str;

      if (result.gender == 1) {
        //document.getElementById('men').value = '';
        $("#men").prop("checked", true);
      } else {
        //document.getElementById('women').value = 0;
        $("#women").prop("checked", true);
      }
      form.render();

      document.getElementById("hiddenphone").value = result.mobile;
      document.getElementById("hiddenemail").value = result.email;
      document.getElementById("hiddenremarks").value = result.remark;
    },
    error: function () {
      console.log("访问失败");
    },
  });
}

function returnmessgae(event) {
  var a = document.getElementsByClassName("typical_contentchange")[0];

  a.style.display = "block";

  var b = document.getElementsByClassName("typical_contentchangeone")[0];

  b.style.display = "none";

  var c = document.getElementsByClassName("typical_content")[0];

  c.style.display = "block";

  var d = document.getElementsByClassName("typical_contentone")[0];

  d.style.display = "none";
}

// function changepassword(event){
// alert('aaaaaaaaa');
// }

/////////////////////////////接口内容

window.onload = function () {
  var http = "http://www.esunrising.net:8099/ords/kzks/api/";

  $(function () {
    function setshowmessage(result) {
      $("#showname").html(result.name);
      $("#showusername").html(result.username);

      var sex = "";
      if (result.gender == 1) {
        sex = "男";
      }
      if (result.gender == 0) {
        sex = "女";
      }
      $("#showusersex").html(sex);
      $("#showrole").html(result.role_str);
      $("#showuserphone").html(result.mobile);
      $("#showuseremail").html(result.email);
      $("#showuserremarks").html(result.remark);
    }

    var data = [];
    $.ajax({
      type: "get",
      url: http + "user/" + localStorage.getItem("user_id"),
      dataType: "json",
      async: true,
      success: function (result) {
        setshowmessage(result);
      },
      error: function () {
        console.log("访问失败");
      },
    });
  });

  // function setshowmessage(result) {
  // 	console.log(result,'result')
  // }

  layui.use("form", function () {
    var form = layui.form;
    //提交
    form.on("submit(changepasswordd)", function (data) {
      // layer.msg(JSON.stringify(data.field));

      var oldpassword = document.getElementsByTagName("input")[0].value;
      var newpassword = document.getElementsByTagName("input")[1].value;
      var nnewpassword = document.getElementsByTagName("input")[2].value;
      console.log(oldpassword);
      console.log(newpassword);
      console.log(nnewpassword);
      if (newpassword !== nnewpassword) {
        layer.msg("两次密码不一样，请重新填写");
      } else {
        $.ajax({
          type: "get",
          url: http + "updatePwd",
          dataType: "json",
          data: {
            old_pwd: oldpassword,
            new_pwd: newpassword,
            user_id: localStorage.getItem("user_id"),
          },
          success: function (Result) {
            console.log(Result);
            if (Result.code == "200") {
              alert("修改密码成功,请重新登录");
              window.location.href = "login.html";
              localStorage.clear();
            } else {
              layer.msg("旧密码不正确，请重新填写");
              // alert("旧密码不正确，请重新填写");
            }
          },
          error: function (data) {
            console.log("失败");
          },
        });
      }
    });
  });
};
